package com.zenni.core;

import com.zenni.utils.DriverUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;

/**
 * Created by douglasj on 2/14/16.
 */
public class WomenFunction {
    private WebDriver driver;
    DriverUtils driverUtils = new DriverUtils();
    public WomenFunction(WebDriver driver) throws InterruptedException{
        this.driver = driver;
    }
    public void goToNewArrivalWomenFrame() throws Exception{
        System.out.println("click the new arrival link");
        if(driver.findElement(By.xpath("//header[@id = 'new-top-navigation']/nav/div/ul/li/button")).isDisplayed()){
            driver.findElements(By.xpath("//a[contains(text(),'New Arrivals')]")).get(0).click();
            Thread thread = new Thread();
            thread.sleep(3000);
        }else{
            driverUtils.waitTillElementExistsByXpath(driver,MainPageVariables.women_new_arrival_xpath_main_page,3000);
            driver.findElement(By.xpath(MainPageVariables.women_new_arrival_xpath_main_page)).click();}
        Assert.assertEquals(driver.getTitle(), "Eyeglass Frames for Women - Popular, Cheap Women's Eyeglasses | Zenni Optical", "You went to wrong page");
    }
    /*public void selectOneFrame(int index) throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver,"//ul[@class = 'categoryList']/li[" + index + "]",3000);
        driver.findElement(By.xpath("//ul[@class = 'categoryList']/li["+index+"]"));
    }
    public void clickOrderButton() throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver,"//a[@id = 'orderGlasses']",3000);
        driver.findElement(By.xpath("//a[@id = 'orderGlasses']"));
    }*/
    public void orderTheFirstFrame() throws Exception{
        System.out.println("select the first frame");
        if(driver.findElement(By.xpath("//header[@id = 'new-top-navigation']/nav/div/ul/li/button")).isDisplayed()){
            driver.findElement(By.xpath("//ul[@class = 'categoryList']/li/div/div/p[@class = 'btnBox']/a[3]")).click();
            Thread thread = new Thread();
            thread.sleep(3000);
        }else {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("document.getElementsByClassName(\"thumbOver\")[0].style.display=\"block\"");
            driverUtils.waitTillElementExistsByXpath(driver, "/html/body/div[1]/div[1]/div/div[2]/div[1]/ul/li[1]/div/div/p[2]/a[3]");
            driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div[2]/div[1]/ul/li[1]/div/div/p[2]/a[3]")).click();
        }
        Assert.assertTrue(driverUtils.isElementExistsByXpath(driver, "//h1[contains(text(),'ORDER YOUR GLASSES')]"));
    }
    public void selectPrescriptionType(String value) throws Exception{
        System.out.println("select the "+value+" as prescriptionType");
        driverUtils.waitTillElementExistsById(driver, "prescriptionType");
        Select prescriptiontype = new Select(driver.findElement(By.id("prescriptionType")));
        prescriptiontype.selectByValue(value);
    }
    public void selectOdSPH(String value) throws Exception{
        System.out.println("select the "+value+" as odSph");
        driverUtils.waitTillElementExistsById(driver,"odSph");
        Select odSPH = new Select(driver.findElement(By.id("odSph")));
        odSPH.selectByValue(value);
    }
    public void selectOSPH(String value) throws Exception{
        System.out.println("select the "+value+" as osSph");
        driverUtils.waitTillElementExistsById(driver,"osSph");
        Select osSPH = new Select(driver.findElement(By.id("osSph")));
        osSPH.selectByValue(value);
    }
    public void selectODCYL(String value) throws Exception{
        System.out.println("select the "+value+" as odCyl");
        driverUtils.waitTillElementExistsById(driver,"odCyl");
        Select odCYL = new Select(driver.findElement(By.id("odCyl")));
        odCYL.selectByValue(value);
    }
    public void selectOSCYL(String value) throws Exception{
        System.out.println("select the "+value+" as osCyl");
        driverUtils.waitTillElementExistsById(driver,"osCyl");
        Select osCYL = new Select(driver.findElement(By.id("osCyl")));
        osCYL.selectByValue(value);
    }
    public void selectODAXIS(String value) throws Exception{
        System.out.println("select the "+value+" as odAxis");
        driverUtils.waitTillElementExistsById(driver,"odAxis");
        Select odAXIS = new Select(driver.findElement(By.id("odAxis")));
        odAXIS.selectByValue(value);
    }
    public void selectOSAXIS(String value) throws Exception{
        System.out.println("select the "+value+" as osAxis");
        driverUtils.waitTillElementExistsById(driver,"osAxis");
        Select osAXIS = new Select(driver.findElement(By.id("osAxis")));
        osAXIS.selectByValue(value);
    }
    public void selectNVADD(String value) throws Exception{
        System.out.println("select the "+value+" as nvAdd");
        driverUtils.waitTillElementExistsById(driver,"nvAdd");
        Select nvADD = new Select(driver.findElement(By.id("nvAdd")));
        nvADD.selectByValue(value);
    }
    public void selectPrismValues(int index) throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver, "//span[@id = 'notEnablePrism']/input["+index+"]");
        driver.findElement(By.xpath("//span[@id = 'notEnablePrism']/input["+index+"]")).click();
    }
    public void selectPD(int index) throws Exception{
        driverUtils.waitTillElementExistsByXpath(driver, "//span[@id = 'spanSignlePD']/input["+index+"]");
        driver.findElement(By.xpath("//span[@id = 'spanSignlePD']/input["+index+"]")).click();
    }
    public void selectPDValue(String value) throws Exception{
        System.out.println("select the "+value+" as pd");
        driverUtils.waitTillElementExistsById(driver,"pdSingle");
        Select pdvalue = new Select(driver.findElement(By.id("pdSingle")));
        pdvalue.selectByValue(value);
    }
    public void finishPrescription() throws Exception{
        System.out.println("click next button");
        driverUtils.waitTillElementExistsById(driver,"prescriptionNext");
        driver.findElement(By.id("prescriptionNext")).click();
        Thread thread = new Thread();
        thread.sleep(3000);
        Assert.assertTrue(driverUtils.isElementExistsByXpath(driver,"//strong[contains(text(),'STANDARD EYEGLASS LENSES')]"));
    }
    public void confirmThePoP() throws Exception{
        driver.findElement(By.xpath("//a[contains(text(),'OK')]")).click();
    }
    public void confirmLens() throws Exception{
        System.out.println("click next button");
        driverUtils.waitTillElementExistsById(driver,"lensSubmitButton");
        driver.findElement(By.id("lensSubmitButton")).click();
        Thread thread = new Thread();
        thread.sleep(3000);
        Assert.assertTrue(driverUtils.isElementExistsByXpath(driver,"//strong[contains(text(),'LENS COATING')]"));
    }
    public void clickAddToCart() throws Exception{
        System.out.println("click add to cart button");
        driverUtils.waitTillElementExistsById(driver,"btnAddToCart");
        driver.findElement(By.id("btnAddToCart")).click();
        Thread thread = new Thread();
        thread.sleep(3000);
        Assert.assertTrue(driver.findElement(By.xpath("//h3[contains(text(),'SHOPPING CART')]")).isDisplayed());
    }
}
